package bkq.viettravel.bll;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import bkq.viettravel.database.SqliteServices;
import bkq.viettravel.dto.ProvinceDTO;
import bkq.viettravel.interf.IObjectCommon;

public class ProvinceBLL implements IObjectCommon<ProvinceDTO>{
	
	SqliteServices servies=null;
	
	/*
	 * constructor 
	*/
	public ProvinceBLL(Context context) {
		servies = new SqliteServices(context);
	}
	

	@Override
	public ArrayList<ProvinceDTO> getAll(String sql) {
		ArrayList<ProvinceDTO> temp = new ArrayList<ProvinceDTO>();
		
		ProvinceDTO province = null;
		
		Cursor c=this.servies.queryData(sql);
		
		if(c != null)
		{
			if(c.moveToFirst())
			{
			 do
			 {
				province=new ProvinceDTO();
				province.setId(Integer.parseInt(c.getString(c.getColumnIndex("id"))));
				province.setProvinceName(c.getString(c.getColumnIndex("name")));

				temp.add(province);
				 
			 }while(c.moveToNext());
		}
		}
		
		return temp.size() == 0? null : temp;
	}
	
	/*
	 * get all Province in Province table
	*/
	@Override
	public ArrayList<ProvinceDTO> getAll() {
		return getAll("select * from Province");
	}

	@Override
	public boolean insert(ProvinceDTO obj) {
		String sql="insert into Province(name) values ('"+obj.getProvinceName()+"')";
		boolean check=false;
		
		try
		{
			this.servies.exeSQL(sql);
			check=true;
		}
		catch(Exception ex)
		{
			Log.d("Insert ProvinceDTo", "Insert Province Error");
			
		}
		return check;
	}

}
